#!/bin/bash


export PATH=$PATH:/home/douglas/llvm/Debug+Asserts/bin/

CURDIR=$(cd $(dirname "$0"); pwd)


#Level 1: Benchmarks (CFP95, CFP2000, CFP2006, etc...)
for level_one in $CURDIR/*
do
  if [ -d $level_one ]; then
    #Level 2: Benchmark programs
    for level_two in $level_one/*
    do
      if [ -d $level_two ]; then
        #If the program was successfully compiled, there is a folder named Output
        #inside of the program folder
        if [ -d $level_two/Output ] ; then
          if [[ "$level_two" == *"433.milc"* ]]; then
            inputfile="/home/douglas/SPEC/speccpu2006/benchspec/CPU2006/433.milc/data/test/input/su3imp.in "
            outputfile=""
            stdoutfile="milc.test.out"
            stderrfile="milc.test.err"
            arguments=" < $inputfile > $stdoutfile 2> $stderrfile "
          elif [[ "$level_two" == *"444.namd"* ]]; then
            inputfile="/home/douglas/SPEC/speccpu2006/benchspec/CPU2006/444.namd/data/all/input/namd.input"
            arguments=" --input $inputfile --iterations 1 --output namd.out >  2> namd.ref.err "
            outputfile="namd.out"
            stdoutfile="namd.all.out"
            stderrfile="namd.all.err"            
            arguments=" --input $inputfile --iterations 1 --output $outputfile 1> $stdoutfile 2> $stderrfile "
          elif [[ "$level_two" == *"447.dealII"* ]]; then
            outputfile=""
            stdoutfile="dealII.test.out"
            stderrfile="dealII.test.err"            
            arguments=" 3 > $stdoutfile 2> $stderrfile "
          elif [[ "$level_two" == *"450.soplex"* ]]; then
            inputfile="/home/douglas/SPEC/speccpu2006/benchspec/CPU2006/450.soplex/data/test/input/test.mps"
            outputfile=""
            stdoutfile="soplex.test.out"
            stderrfile="soplex.test.err"             
            arguments="-m1200 $(basename "$inputfile") > $stdoutfile 2> $stderrfile "
          elif [[ "$level_two" == *"470.lbm"* ]]; then
            inputfile="/home/douglas/SPEC/speccpu2006/benchspec/CPU2006/470.lbm/data/test/input/100_100_130_cf_a.of"
            outputfile=""
            stdoutfile="lbm.ref.out"
            stderrfile="lbm.ref.err"            
            arguments="1000 reference.dat 0 0 $inputfile > $stdoutfile 2> $stderrfile "
          elif [[ "$level_two" == *"400.perlbench"* ]]; then
            inputfile="/home/douglas/SPEC/speccpu2006/benchspec/CPU2006/400.perlbench/data/all/input/diffmail.pl"
            outputfile=""
            stdoutfile="perlbench.all.diffmail.out"
            stderrfile="dperlbench.ref.diffmail.err"            
            arguments=" -I./lib $inputfile 4 800 10 17 19 300 > $stdoutfile 2> $stderrfile "
          elif [[ "$level_two" == *"401.bzip2"* ]]; then
            inputfile="/home/douglas/SPEC/speccpu2006/benchspec/CPU2006/401.bzip2/data/all/input/input.program"
            outputfile=""
            stdoutfile="bzip2.all.program.out"
            stderrfile="bzip2.all.program.err"            
            arguments=" $inputfile 280 > $stdoutfile 2> $stderrfile "
          elif [[ "$level_two" == *"429.mcf"* ]]; then
            inputfile="/home/douglas/SPEC/speccpu2006/benchspec/CPU2006/429.mcf/data/test/input/inp.in"
            outputfile=""
            stdoutfile="mcf.test.out"
            stderrfile="mcf.test.err"            
            arguments=" $inputfile > $stdoutfile 2> $stderrfile "
#          elif [[ "$level_two" == *"403.gcc"* ]]; then
#            inputfile="/home/douglas/SPEC/speccpu2006/benchspec/CPU2006/403.gcc/data/test/input/cccp.i"
#            outputfile=""
#            stdoutfile="gcc.test.cccp.out"
#            stderrfile="gcc.test.cccp.err"            
#            arguments=" $inputfile -o cccp.s > $stdoutfile 2> $stderrfile "
          elif [[ "$level_two" == *"445.gobmk"* ]]; then
            inputfile="/home/douglas/SPEC/speccpu2006/benchspec/CPU2006/445.gobmk/data/test/input/connection_rot.tst"
            outputfile=""
            stdoutfile="gobmk.connection_rot.out"
            stderrfile="gobmk.connection_rot.err"            
            arguments=" --quiet --mode gtp < $inputfile > $stdoutfile 2> $stderrfile "
          elif [[ "$level_two" == *"456.hmmer"* ]]; then
            inputfile="/home/douglas/SPEC/speccpu2006/benchspec/CPU2006/456.hmmer/data/test/input/bombesin.hmm"
            outputfile=""
            stdoutfile="hmmer.test.bombesin.out"
            stderrfile="hmmer.test.bombesin.err"  
            arguments=" --fixed 0 --mean 325 --num 45000 --sd 200 --seed 0 $(basename "$inputfile") > $stdoutfile 2> $stderrfile "
          elif [[ "$level_two" == *"458.sjeng"* ]]; then
            inputfile="/home/douglas/SPEC/speccpu2006/benchspec/CPU2006/458.sjeng/data/test/input/test.txt"
            outputfile=""
            stdoutfile="sjeng.test.out"
            stderrfile="sjeng.test.err"            
            arguments=" $inputfile > $stdoutfile 2> $stderrfile "
          elif [[ "$level_two" == *"462.libquantum"* ]]; then
            outputfile=""
            stdoutfile="libquantum.test.out"
            stderrfile="libquantum.test.err"          
            arguments=" 33 5 > $stdoutfile 2> $stderrfile "
          elif [[ "$level_two" == *"464.h264ref"* ]]; then
            inputfile="/home/douglas/SPEC/speccpu2006/benchspec/CPU2006/464.h264ref/data/test/input/foreman_test_encoder_baseline.cfg"
            outputfile=""
            stdoutfile="h264ref.test.foreman_baseline.out"
            stderrfile="h264ref.test.foreman_baseline.err"            
            arguments=" -d $(basename "$inputfile") > $stdoutfile 2> $stderrfile "
          elif [[ "$level_two" == *"471.omnetpp"* ]]; then
            inputfile="/home/douglas/SPEC/speccpu2006/benchspec/CPU2006/471.omnetpp/data/test/input/omnetpp.ini"
            outputfile=""
            stdoutfile="omnetpp.test.log"
            stderrfile="omnetpp.test.err"            
            arguments=" $(basename "$inputfile") > $stdoutfile 2> $stderrfile "
          elif [[ "$level_two" == *"473.astar"* ]]; then
            inputfile="/home/douglas/SPEC/speccpu2006/benchspec/CPU2006/473.astar/data/test/input/lake.cfg"
            outputfile=""
            stdoutfile="astar.test.lake.out"
            stderrfile="astar.test.lake.err"
            arguments=" $(basename "$inputfile") > $stdoutfile 2> $stderrfile "
          elif [[ "$level_two" == *"483.xalancbmk"* ]]; then
            INPUTFOLDER="/home/douglas/SPEC/speccpu2006/benchspec/CPU2006/483.xalancbmk/data/test/input"
            inputfile1="$INPUTFOLDER/test.xml"
            inputfile2="$INPUTFOLDER/xalanc.xsl"
            outputfile=""
            stdoutfile="xalancbmk.test.out"
            stderrfile="xalancbmk.test.err"       
            arguments=" -v $inputfile1 $inputfile2 > $stdoutfile 2> $stderrfile "
          else
            inputfile=""
            outputfile=""
            stdoutfile=""
            stderrfile=""
            arguments=""
        fi 

        for file in $level_two/Output/*.linked.rbc
        do            
          if [ -f $file ] && [ "$arguments" != "" ]; then              
            filename=$(basename "$file")              
            cd $level_two/Output/
            echo "Preparing $filename file..."
            opt -mem2reg -loop-simplify -load /home/douglas/llvm/Debug+Asserts/lib/LLVMIterationPredictor.so -iteration-predictor $filename -o $filename.opt.bc -stats -debug
            llc $filename.opt.bc -o $filename.s
            g++ $filename.s -o $filename.exe
            command="./$filename.exe $arguments"
            echo "Running $filename.exe with the arguments $arguments"
            eval $command;
          fi              
        done                  
        fi        
      fi      
    done  
  fi  
done



